package com.adaptiveAgingAdmin.security.config;

import com.adaptiveAgingAdmin.security.exception.AuthAccessDeniedHandler;
import com.adaptiveAgingAdmin.security.exception.AuthEntryPointHandler;
import com.adaptiveAgingAdmin.security.filter.TokenAuthenticationFilter;
import com.adaptiveAgingAdmin.security.manager.RoleAuthorizationManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
public class SecurityConfig{
    @Autowired
    private RoleAuthorizationManager authorizationManager;
    @Autowired
    private AuthEntryPointHandler authEntryPointHandler;
    @Autowired
    private AuthAccessDeniedHandler accessDeniedHandler;
    @Bean
    SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.addFilterBefore(new TokenAuthenticationFilter(),UsernamePasswordAuthenticationFilter.class);
        httpSecurity
                // 关闭csrf, 前后端分离不需要csrf保护
                .csrf().disable()
                // 不通过session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeHttpRequests()
                // 允许public下所有接口匿名访问
                .antMatchers("/public/*").permitAll()
                // 除上面之外所有请求要进行认证
                .anyRequest().access(authorizationManager)
                // 登录异常处理和权限异常处理
                .and()
                .exceptionHandling().authenticationEntryPoint(authEntryPointHandler).accessDeniedHandler(accessDeniedHandler);
        return httpSecurity.build();
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Bean
    public ReloadableResourceBundleMessageSource messageSource(){
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:org/springframework/security/messages_zh_CN");
        return messageSource;
    }
}
